$.extend($.fn.tabs.methods, {
    /**
     * 绑定双击事件
     * @param {Object} jq
     * @param {Object} caller 绑定的事件处理程序
     */
    bindDblClick: function (jq, caller) {
        return jq.each(function () {
            var that = this;
            $(this).children("div.tabs-header").find("ul.tabs").undelegate('li', 'dblclick.tabs').delegate('li', 'dblclick.tabs', function (e) {
                if (caller && typeof(caller) == 'function') {
                    var title = $(this).text();
                    var index = $(that).tabs('getTabIndex', $(that).tabs('getTab', title));
                    caller(index, title);
                }
            });
        });
    },
    /**
     * 解除绑定双击事件
     * @param {Object} jq
     */
    unbindDblClick: function (jq) {
        return jq.each(function () {
            $(this).children("div.tabs-header").find("ul.tabs").undelegate('li', 'dblclick.tabs');
        });
    }
});

//添加一个选项卡面板
function addTabs(title, url, icon) {
    if (url != undefined) {
        var mainTabs = $("#mainTabs");
        if (!mainTabs.tabs("exists", title)) {
            var options = {
                title: title,
                content: "<iframe src='" + url + "' frameBorder=0 border=0 scrolling=no style='width: 100%; height: 100%;'/>",
                closable: true,
                iconCls: icon,
                tools: [{
                    iconCls: "icon-mini-refresh",
                    handler: function () {
                        var selectedTab = mainTabs.tabs("getSelected");  // 获取选择的面板
                        mainTabs.tabs("update", {
                            tab: selectedTab,
                            options: {
                                href: url
                            }
                        });
                    }
                }]
            };
            mainTabs.tabs("add", options);
        } else {
            mainTabs.tabs("select", title);
        }
        mainTabs.tabs("bindDblClick", onDblClickOnTabClose);
    }
}

//添加一个新的页签
function addNewTab(title, url, icon) {
    if (url != undefined) {
        var mainTabs = parent.$("#mainTabs");
        if (!mainTabs.tabs("exists", title)) {
            mainTabs.tabs("add", {
                title: title,
                content: "<iframe src='" + url + "' frameBorder=0 border=0 scrolling=no style='width: 100%; height: 99.6%;'/>",
                closable: true,
                iconCls: icon
            });
        } else {
            mainTabs.tabs("select", title);
        }
    }
}

//关闭当前选中的页签
function closeCurrTab() {
    var mainTabs = parent.parent.$("#mainTabs");
    var currentTab = mainTabs.tabs("getSelected");
    var index = mainTabs.tabs("getTabIndex", currentTab);
    mainTabs.tabs("close", index);
}

/**
 * 生成eadyui能解析的有层次关系的树的json对象(全部节点打开)
 * @param rows
 * @returns {Array}
 */
function makeEasyTree(rows) {
    function exists(rows, pid) {
        for (var i = 0; i < rows.length; i++) {
            if (rows[i].id == pid) return true;
        }
        return false;
    }

    var nodes = [];
    // 得到顶层节点
    for (var i = 0; i < rows.length; i++) {
        var row = rows[i];
        if (!exists(rows, row.pid)) {
            nodes.push({
                id: row.id,
                text: row.text,
                pid: row.pid,
                iconCls: row.iconCls,
                url: row.url
            });
        }
    }

    var toDo = [];
    for (var i = 0; i < nodes.length; i++) {
        toDo.push(nodes[i]);
    }
    while (toDo.length) {
        var node = toDo.shift();    // 父节点
        // 得到子节点
        for (var i = 0; i < rows.length; i++) {
            var row = rows[i];
            if (row.pid == node.id) {
                var child = {id: row.id, text: row.text, pid: row.pid, iconCls: row.iconCls, url: row.url};
                if (node.children) {
                    node.children.push(child);
                } else {
                    node.children = [child];
                }
                toDo.push(child);
            }
        }
    }
    return nodes;
}

/**
 * 双击tab页签关闭当前页签
 * @param index
 * @param title
 */
function onDblClickOnTabClose(index, title) {
    if (parseInt(index) > 0) {
        var mainTabs = $("#mainTabs");
        mainTabs.tabs("close", index)
    }
}

/**
 * 重写easyui input样式
 */
function rewriteInputStyle() {
    // input输入框样式重写
    var ehomeInput = $(".ehome-input");
    if (ehomeInput.length > 0) {
        $.each(ehomeInput.siblings(), function (i, e) {
            $(e).attr("style", "height:26px;line-height:26px;width:200px");
            $(e).find("input").attr("style", "height:26px;line-height:26px;width:100%;padding-left:5px;padding-top:0;padding-bottom:0;");
            if ($(".textbox-icon").length > 0) {    // 如果有图标
                $(e).find(".textbox-icon").attr("style", "height:26px;line-height:26px;")
            }
        });
    }
}

/**
 * 重写easyui textarea样式
 */
function rewriteTextareaStyle() {
    // textarea输入框样式重写
    var ehomeTextarea = $(".ehome-textarea");
    if (ehomeTextarea.length > 0) {
        $.each(ehomeTextarea.siblings(), function (i, e) {
            $(e).attr("style", "height:100px;line-height:100px;width:336px");
            $(e).find("textarea").attr("style", "height:98%;width:97%;padding-left:5px;");
        });
    }
}

/**
 * 重写easyui select样式
 */
function rewriteSelectStyle() {
    // select输入框样式重写
    var ehomeSelect = $(".ehome-select");
    if (ehomeSelect.length > 0) {
        $.each(ehomeSelect.siblings(), function (i, e) {
            $(e).attr("style", "height:26px;line-height:26px;width:200px");
            $(e).find("span>a").attr("style", "width:18px;height:26px;");
            $(e).find("input").attr("style", "height:26px;line-height:26px;width:100%;padding-left:5px;padding-top:0;padding-bottom:0;");
        });
    }
}

/**
 * 重写dom元素样式
 */
function rewriteDomElementStyle() {
    rewriteInputStyle();
    rewriteSelectStyle();
    rewriteTextareaStyle()
}

/*****************************html转义相关函数*****************************/
// 普通字符转换成转意符
function html2Escape(sHtml) {
    return sHtml.replace(/[<>&"]/g, function (c) {
        return {'<': '&lt;', '>': '&gt;', '&': '&amp;', '"': '&quot;'}[c];
    });
}

// 转意符换成普通字符
function escape2Html(str) {
    var arrEntities = {'lt': '<', 'gt': '>', 'nbsp': ' ', 'amp': '&', 'quot': '"'};
    return str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function (all, t) {
        return arrEntities[t];
    });
}

// 回车转为br标签
function return2Br(str) {
    return str.replace(/\r?\n/g, "<br />");
}

// &nbsp;转成空格
function nbsp2Space(str) {
    var arrEntities = {'nbsp': ' '};
    return str.replace(/&(nbsp);/ig, function (all, t) {
        return arrEntities[t]
    })
}

// rgb颜色值转成16进制的值
function rgbToHex(rgb) {
    var regexp = /^rgb\(([0-9]{0,3}),\s([0-9]{0,3})\,\s([0-9]{0,3})\)/g;
    var re = rgb.replace(regexp, "$1 $2 $3").split(" ");//利用正则表达式去掉多余的部分
    var hexColor = "#";
    var hex = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
    for (var i = 0; i < 3; i++) {
        var r = null;
        var c = re[i];
        var hexAr = [];
        while (c > 16) {
            r = c % 16;
            c = (c / 16) >> 0;
            hexAr.push(hex[r]);
        }
        hexAr.push(hex[c]);
        hexColor += hexAr.reverse().join('');
    }
    return hexColor;
}